#ifndef GRAPHPLOT_H
#define GRAPHPLOT_H

#include <QDialog>
#include <QTimer>
#include "qnode.hpp"
#include "graph_setting.h"
#include "qcustomplot.h"
#include "paramstuning_window.h"
#include <queue>
#include <vector>
#include <QThread>
#include <QList>
#include <QTimer>
#include <QMutex>
#include <QDoubleSpinBox>
#include <QMouseEvent>

typedef struct{
    QString name;
    float value;
    double stamp;
}PointType;

namespace Ui {
class graphplot;
}

namespace alphadog_monitor {

class GraphThread : public QObject
{
    Q_OBJECT
public:
    GraphThread(int iCount, QList<QCustomPlot*>* list, std::vector<std::vector<QDoubleSpinBox*>>* scales, QNode* _node);
    ~GraphThread();
    void createItem();
    void startMultThread(int dt);
    bool enabled[4];

Q_SIGNALS:
    void plotGraph(int);


public Q_SLOTS:
    void realtimeGraph1(void){updateGraphForm(0);}
    void realtimeGraph2(void){updateGraphForm(1);}
    void realtimeGraph3(void){updateGraphForm(2);}
    void realtimeGraph4(void){updateGraphForm(3);}
    void slot_updateGraphData(float*, QString*);


private:
    int m_iThreadCount;//开启的线程个数
    QList<QCustomPlot*>* graphs;
    std::vector<std::vector<QDoubleSpinBox*>>* scales;
    QNode* qnode;
    QList<QTimer*> m_qTimerList;
    QList<QThread*> m_threadList;

    std::vector<std::vector<QList<PointType>>> graph_container;

    bool Timer_firstRun = true;
    double Timer_startTime;
    int dtGraph; // ms
    bool clearflag[4] ={false, false, false, false};
    void updateGraphForm(int type);
    void setClearFlag(int num, bool flag){
        QMutex locker;
        locker.lock();
        clearflag[num] = flag;
        locker.unlock();
    }
    QReadWriteLock rwlocker;
    QMutex locker[4];
    QMutex ros_locker;

};

class graphplot : public QDialog
{
    Q_OBJECT

public:
    explicit graphplot(QWidget *parent = 0, QNode* _node = nullptr);
    ~graphplot();

public Q_SLOTS:
    void slot_plotGraph(int);

protected:
    void mousePressEvent(QMouseEvent *event);//鼠标点击
    void mouseMoveEvent(QMouseEvent *event);//鼠标移动事件
    void mouseReleaseEvent(QMouseEvent *event);//鼠标释放事件
    QPoint m_offPos;
private:
    Ui::graphplot *ui;
    QWidget* _parent;
    graph_setting* setinf;
    paramstuning_window* params_tune;
    QList<QCustomPlot*> graphList;
    std::vector<std::vector<QDoubleSpinBox*>> graph_scales;
    std::vector<std::vector<QCheckBox*>> graph_checkboxes;
    bool enabled[4]{false, false, false, false};
    GraphThread* graph_threads;
    void initWidget(void);
    void enableGraph(QPushButton* btn, int index);

};

}

#endif // GRAPHPLOT_H
